package com.chzc.fm.duotin.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.chzc.fm.duotin.bean.mine.SpecialBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by BillSinla on 2016/7/6.
 */
public class ContentDao {
    private HistoryHelper historyHelper = null;
    public ContentDao(Context context){
        historyHelper = new HistoryHelper(context);
    }

    public void insertContent(SpecialBean.ContentListBean content){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("insert into content(" +
                "content_id, display_order, title, album_id," +
                "updated, audio_32_size, audio_64_size, duration, " +
                "play_num, audio_32_url, audio_64_url, image_url) " +
                "values(?,?,?,?,?,?,?,?,?,?,?,?)",
                new String[]{content.getId(), content.getDisplay_order(),
                        content.getTitle(), content.getAlbum_id(),
                        content.getUpdated(), content.getAudio_32_size(),
                        content.getAudio_64_size(), content.getDuration(),
                        content.getPlay_num(), content.getAudio_32_url(),
                        content.getAudio_64_url(), content.getImage_url()});
        db.close();
    }

    public void deleteContent(String contentId){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("delete from content where content_id = ?",
                new String[]{contentId});
        db.close();
    }

    public void deleteContentByAlbum(String albumId){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("delete from content where album_id = ?",
                new String[]{albumId});
        db.close();
    }

    public void updateContent(SpecialBean.ContentListBean content){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        db.execSQL("update content set updated = ? where content_id = ?",
                new String[]{content.getUpdated(), content.getId()});
        db.close();
    }

    public List<SpecialBean.ContentListBean> getContent(String album_id){
        SQLiteDatabase db = historyHelper.getWritableDatabase();
        List<SpecialBean.ContentListBean> contentList = new ArrayList<SpecialBean.ContentListBean>();

        Cursor cursor = db.rawQuery("select * from content where album_id = ?",
                new String[]{album_id});

        while(cursor.moveToNext()){
            SpecialBean.ContentListBean content = new SpecialBean.ContentListBean();
            content.setId(cursor.getString(cursor.getColumnIndex("content_id")));
            content.setDisplay_order(cursor.getString(cursor.getColumnIndex("display_order")));
            content.setTitle(cursor.getString(cursor.getColumnIndex("title")));
            content.setAlbum_id(cursor.getString(cursor.getColumnIndex("album_id")));
            content.setUpdated(cursor.getString(cursor.getColumnIndex("updated")));
            content.setAudio_32_size(cursor.getString(cursor.getColumnIndex("audio_32_size")));
            content.setAudio_64_size(cursor.getString(cursor.getColumnIndex("audio_64_size")));
            content.setDuration(cursor.getString(cursor.getColumnIndex("duration")));
            content.setPlay_num(cursor.getString(cursor.getColumnIndex("play_num")));
            content.setAudio_32_url(cursor.getString(cursor.getColumnIndex("audio_32_url")));
            content.setAudio_64_url(cursor.getString(cursor.getColumnIndex("audio_64_url")));
            content.setImage_url(cursor.getString(cursor.getColumnIndex("image_url")));
            contentList.add(content);
        }
        cursor.close();
        db.close();

        return contentList;
    }

    public boolean isExistByAlbum(String album_id){
        SQLiteDatabase db = historyHelper.getWritableDatabase();

        Cursor cursor = db.rawQuery("select * from content where album_id = ?",
                new String[]{album_id});
        boolean exists = cursor.moveToNext();

        cursor.close();
        db.close();
        return exists;
    }

    public boolean isExist(String content_id){
        SQLiteDatabase db = historyHelper.getWritableDatabase();

        Cursor cursor = db.rawQuery("select * from content where content_id = ?",
                new String[]{content_id});
        boolean exists = cursor.moveToNext();

        cursor.close();
        db.close();
        return exists;
    }
}
